/***********************************************************************************
**  
* @copyright (c) 2010-2019, ChengDu Duyer Technology Co., LTD. All Right Reserved.
*
************************************************************************************/
/**
* @file	    mjs_web_auth_action.cpp
* @version     
* @brief      
* @author   duye
* @date	    2016-12-26
* @note 
*
* 1. 2016-12-26 duye Created this file
*/

#include <string>
#include <duye_type.h>
#include <duye_logger.h>
#include <duye_cfg_mgr.h>

#include <mj_net_protocol.h>
#include <mjs_inner_msg.h>
#include <mjs_mysql.h>
#include <mjs_reqer.h>
#include <mjs_web_auth_action.h>

static const int8* DUYE_LOG_PREFIX = "mangjings.action.web_auth";

MJSWebAuthAction::MJSWebAuthAction() {}
MJSWebAuthAction::~MJSWebAuthAction() {}

bool MJSWebAuthAction::onAction(MJMsgAction* action, MJSReqer* reqer) {
	if (!action || !reqer) {
		DUYE_ERROR("action == NULL || reqer == NULL");
		notify(reqer, MJS_INNER_AUTH_FA);
		return false;
	}

	if (action->getName() != "web_auth") {
		DUYE_ERROR("action_name != 'web_auth");
		notify(reqer, MJS_INNER_AUTH_FA);
		return false;
	}

	std::string client_ip = action->getValue("ip");
	std::string user = action->getValue("user");
	std::string pwdmd5 = action->getValue("pwdmd5");

	DUYE_DEBUG("request >> client_ip=%s user=%s pwdmd5=%s", client_ip.c_str(), user.c_str(), pwdmd5.c_str());

	std::string config_web_ip;
	std::string config_web_user;
	std::string config_web_pwdmd5;
	if (!duye::CfgMgr::ins().getValue("node:mjw.param:ip", config_web_ip, "value")) {
		DUYE_ERROR("get mjw config ip failed");
		notify(reqer, MJS_INNER_AUTH_FA, user.c_str());
		return false;
	}

	if (!duye::CfgMgr::ins().getValue("node:mjw.param:username", config_web_user, "value")) {
		DUYE_ERROR("get mjw config username failed");
		notify(reqer, MJS_INNER_AUTH_FA, user.c_str());
		return false;
	}	

	if (!duye::CfgMgr::ins().getValue("node:mjw.param:pwdmd5", config_web_pwdmd5, "value")) {
		DUYE_ERROR("get mjw config pwdmd5 failed");
		notify(reqer, MJS_INNER_AUTH_FA, user.c_str());
		return false;
	}

	DUYE_DEBUG("config >> client_ip=%s user=%s pwdmd5=%s", config_web_ip.c_str(), config_web_user.c_str(), config_web_pwdmd5.c_str());

	bool result = true;
	std::string desc;
	if (client_ip != config_web_ip) {
		DUYE_ERROR("client_ip != config_web_ip");
		desc = "web ip address error";
		result = false;
	}

	if (result && user != config_web_user) {
		DUYE_ERROR("user != config_web_user");
		desc = "web user name error";
		result = false;
	}	

	if (result && pwdmd5 != config_web_pwdmd5) {
		DUYE_ERROR("pwdmd5 != config_web_pwdmd5");
		desc = "web password error";
		result = false;
	}
	
	// response client
	if (!response(reqer, action->getName(), user, result, desc)) {
		DUYE_ERROR("send web_auth response error");
		result = false;
	}

	if (result) {
		notify(reqer, MJS_INNER_AUTH_OK, user.c_str());
	} else {
		notify(reqer, MJS_INNER_AUTH_FA, user.c_str());
	}
	

	DUYE_INFO("web service %s with user:%s auth successed", client_ip.c_str(), user.c_str());

	return true;
}

bool MJSWebAuthAction::response(MJSReqer* reqer, const std::string& action_name, const std::string& user, const bool result, const std::string& desc) {
	MJMsgAction resp_action;
	if (!MJMsgFactory::ins().createAction(action_name, resp_action, MJMsgFactory::MJ_PEER_WEB)) {
		DUYE_ERROR("createAction 'web_auth' failed");
		notify(reqer, MJS_INNER_AUTH_FA, user.c_str());
		return false;
	}

	resp_action.setValue("user", user);
	if (result) {
		resp_action.setValue("result", "yes");
	} else {
		resp_action.setValue("result", "no");
	}		
	resp_action.setValue("error", desc);

	std::string msg_xml;
	MJMsgFactory::ins().createMsgXml(resp_action, msg_xml);
	DUYE_DEBUG("%s", msg_xml.c_str());
	if (!reqer->sendToClient(msg_xml)) {
		DUYE_ERROR("sendToClient failed");
		return false;
	}

	return true;
}

bool MJSWebAuthAction::notify(MJSReqer* reqer, MJSInnerMsgType type, const int8* data) {
	if (!reqer->notify(type, (void*)(data))) {
		DUYE_ERROR("notify failed");
		return false;
	}

	return true;
}